package com.helen.ui.order;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.helen.db.UserHelper;
import com.helen.entity.OrderDetailEntity;
import com.helen.entity.OrderListEntity;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.ImageLoadUtil;
import com.helen.utils.JsonUtil;
import com.helen.utils.MyLog;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.cache.converter.SerializableDiskConverter;
import com.zhouyou.http.cache.model.CacheMode;
import com.zhouyou.http.cache.model.CacheResult;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import java.io.Serializable;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yang on 2018/5/28.
 * 订单详情
 */

public class OrderDetailActivity extends BaseActivity {
    public static final int COMMENTSUCCESS = 0x33;
    @BindView(R.id.tv_order_status)
    TextView tvOrderStatus;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_detail_address)
    TextView tvDetailAddress;
    @BindView(R.id.img_address_right)
    ImageView imgAddressRight;
    @BindView(R.id.img_product)
    ImageView imgProduct;
    @BindView(R.id.tv_product_name)
    TextView tvProductName;
    @BindView(R.id.tv_product_spec)
    TextView tvProductSpec;
    @BindView(R.id.tv_product_price)
    TextView tvProductPrice;
    @BindView(R.id.tv_product_count)
    TextView tvProductCount;
    @BindView(R.id.tv_user_jinbao)
    TextView tvUserJinbao;
    @BindView(R.id.ll_jinbao)
    LinearLayout llJinbao;
    @BindView(R.id.view_jibao_line)
    View viewJibaoLine;
    @BindView(R.id.tv_daijin)
    TextView tvDaijin;
    @BindView(R.id.ll_daijin)
    LinearLayout llDaijin;
    @BindView(R.id.view_daijin_line)
    View viewDaijinLine;
    @BindView(R.id.tv_express_price)
    TextView tvExpressPrice;
    @BindView(R.id.tv_note)
    TextView tvNote;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.ll_address)
    LinearLayout llAddress;
    @BindView(R.id.tv_order_number)
    TextView tvOrderNumber;
    @BindView(R.id.tv_order_create_time)
    TextView tvOrderCreateTime;
    @BindView(R.id.tv_expressnum)
    TextView tvExpressnum;
    @BindView(R.id.tv_order_send_time)
    TextView tvOrderSendTime;
    @BindView(R.id.tv_order_confirm_time)
    TextView tvOrderConfirmTime;
    @BindView(R.id.ll_order_bottom)
    LinearLayout llOrderBottom;
    @BindView(R.id.tv_bottom_order_left)
    TextView tvBottomOrderLeft;
    @BindView(R.id.tv_bottom_order_right)
    TextView tvBottomOrderRight;
    private String id;
    private OrderDetailEntity orderDetailEntity;
    private int status = 1;
    private HandleOrderStatus handleOrderStatus;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_order_detail;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.order_detail);
        handleOrderStatus = new HandleOrderStatus(OrderDetailActivity.this);
        imgAddressRight.setVisibility(View.GONE);
        id = getIntent().getStringExtra("id");
        orderDetailApi();

        handleOrderStatus.setOnNavToOrderActivity(new HandleOrderStatus.NavToOrderActivityListener() {
            @Override
            public void setOnNavToOrderActivityListener() {
                finish();
            }
        });
    }


    @Override
    protected void updateViews() {

    }

    private void orderDetailApi() {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("id", UserHelper.getInstance().getloginEntity(OrderDetailActivity.this).getId() + "");
        params.put("sid", id);
        EasyHttp.post(Constants.API_JSONAPI_ORDER_DETAIL)
                .cacheMode(CacheMode.CACHEANDREMOTEDISTINCT)
                .cacheKey(Constants.API_JSONAPI_ORDER_DETAIL + "_" + id)
                .cacheDiskConverter(new SerializableDiskConverter())
                .params(params)
                .execute(new SimpleCallBack<CacheResult<String>>() {

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.d(TAG, "获取订单详情失败==" + e.toString());
                        MToast.makeTextShort(OrderDetailActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(CacheResult<String> stringCacheResult) {
                        MyLog.e("yang", "订单详情==" + stringCacheResult);
                        int code = JsonUtil.getJsonValuesInt(stringCacheResult.data, "code");
                        String msg = JsonUtil.getJsonValuesString(stringCacheResult.data, "msg");
                        if (code == 0) {
                            Gson gson = new GsonBuilder().create();
                            orderDetailEntity = gson.fromJson(stringCacheResult.data, OrderDetailEntity.class);
                            status = orderDetailEntity.getState();
                            //    0、全部 1、待付款 2、待发货 3、待收货 4、待评价 5、交易完成 6、已删除 7、已取消
                            switch (status) {
                                case 0:
                                    break;
                                case 1:
                                    tvOrderStatus.setText("订单状态: 待付款");
                                    tvBottomOrderLeft.setVisibility(View.VISIBLE);
                                    tvBottomOrderRight.setVisibility(View.VISIBLE);
                                    tvBottomOrderLeft.setText("取消订单");
                                    tvBottomOrderRight.setText("付款");
                                    break;
                                case 2:
                                    tvOrderStatus.setText("订单状态: 待发货");
                                    tvBottomOrderRight.setVisibility(View.GONE);
//                                    tvBottomOrderLeft.setText("查看物流");
                                    tvBottomOrderLeft.setVisibility(View.GONE);
                                    llOrderBottom.setVisibility(View.GONE);
                                    break;
                                case 3:
                                    tvOrderStatus.setText("订单状态: 待收货");
                                    tvBottomOrderRight.setVisibility(View.VISIBLE);
                                    tvBottomOrderRight.setText("确认收货");
//                                    tvBottomOrderLeft.setText("查看物流");
                                    tvBottomOrderLeft.setVisibility(View.GONE);
                                    tvOrderSendTime.setVisibility(View.VISIBLE);
                                    tvOrderConfirmTime.setVisibility(View.VISIBLE);


                                    tvOrderSendTime.setText("发货时间: " + orderDetailEntity.getSend_time());
                                    tvOrderConfirmTime.setText("收货时间: " + orderDetailEntity.getConfirm_time());
                                    break;
                                case 4:
                                    tvOrderStatus.setText("订单状态: 待评价");
                                    tvBottomOrderRight.setText("去评价");
                                    tvBottomOrderRight.setVisibility(View.VISIBLE);
//                                    tvBottomOrderLeft.setText("查看物流");
                                    tvBottomOrderLeft.setVisibility(View.GONE);

                                    tvOrderSendTime.setVisibility(View.VISIBLE);
                                    tvOrderConfirmTime.setVisibility(View.VISIBLE);
                                    tvOrderSendTime.setText("发货时间: " + orderDetailEntity.getSend_time());
                                    tvOrderConfirmTime.setText("收货时间: " + orderDetailEntity.getConfirm_time());
                                    break;
                                case 5:
                                    tvOrderStatus.setText("订单状态: 交易完成");
                                    tvBottomOrderLeft.setVisibility(View.VISIBLE);
                                    tvBottomOrderLeft.setText("删除订单");
                                    tvBottomOrderRight.setText("查看评价");
                                    tvBottomOrderRight.setVisibility(View.VISIBLE);

                                    tvOrderSendTime.setVisibility(View.VISIBLE);
                                    tvOrderConfirmTime.setVisibility(View.VISIBLE);
                                    tvOrderSendTime.setText("发货时间: " + orderDetailEntity.getSend_time());
                                    tvOrderConfirmTime.setText("收货时间: " + orderDetailEntity.getConfirm_time());
                                    break;
                                case 6:
                                    tvOrderStatus.setText("订单状态: 已删除");
                                    tvBottomOrderLeft.setVisibility(View.GONE);
                                    tvBottomOrderRight.setVisibility(View.GONE);

                                    tvOrderSendTime.setVisibility(View.VISIBLE);
                                    tvOrderConfirmTime.setVisibility(View.VISIBLE);
                                    tvOrderSendTime.setText("发货时间: " + orderDetailEntity.getSend_time());
                                    tvOrderConfirmTime.setText("收货时间: " + orderDetailEntity.getConfirm_time());
                                    break;
                                case 7:
                                    tvOrderStatus.setText("订单状态: 已取消");
                                    llOrderBottom.setVisibility(View.GONE);
                                    break;

                            }

                            tvName.setText("收货人: " + orderDetailEntity.getName());
                            tvPhone.setText(orderDetailEntity.getPhone());
                            tvDetailAddress.setText("收货地址: " + orderDetailEntity.getCity() + orderDetailEntity.getDetail());

                            ImageLoadUtil.displayImage(OrderDetailActivity.this, imgProduct, orderDetailEntity.getProductimg(), R.mipmap.app_logo, R.mipmap.app_logo);
                            tvProductName.setText(orderDetailEntity.getProductname());
                            tvProductSpec.setText(orderDetailEntity.getSpec());
                            tvProductPrice.setText("￥" + orderDetailEntity.getOprice());
                            tvProductCount.setText("x" + orderDetailEntity.getCount());

                            if (orderDetailEntity.getJinbao() == 0) {
                                llJinbao.setVisibility(View.GONE);
                                viewJibaoLine.setVisibility(View.GONE);
                            } else {
                                llJinbao.setVisibility(View.VISIBLE);
                                viewJibaoLine.setVisibility(View.VISIBLE);
                                tvUserJinbao.setText(orderDetailEntity.getJinbao() + "");
                            }


                            if (TextUtils.isEmpty(orderDetailEntity.getDiscard())) {
                                llDaijin.setVisibility(View.GONE);
                                viewDaijinLine.setVisibility(View.GONE);
                            } else {
                                llDaijin.setVisibility(View.VISIBLE);
                                viewDaijinLine.setVisibility(View.VISIBLE);
                                tvDaijin.setText(orderDetailEntity.getDiscard() + "");
                            }

                            tvExpressPrice.setText("￥" + orderDetailEntity.getExpressprice());
                            tvNote.setText(orderDetailEntity.getMemo());
                            tvAmount.setText("￥" + String.format("%.2f",orderDetailEntity.getPrice()));
                            tvOrderNumber.setText("订单编号: " + orderDetailEntity.getId());
                            tvOrderCreateTime.setText("下单时间: " + orderDetailEntity.getCreatetime());

                            if(!TextUtils.isEmpty(orderDetailEntity.getExpressnum())){
                                tvExpressnum.setVisibility(View.VISIBLE);
                                tvExpressnum.setText("快递单号:  "+orderDetailEntity.getExpressnum());
                            }else {
                                tvExpressnum.setVisibility(View.GONE);
                            }
                        } else {
                            MToast.makeTextShort(OrderDetailActivity.this, msg).show();
                        }
                    }
                });

    }


    @OnClick({R.id.tv_bottom_order_left, R.id.tv_bottom_order_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_bottom_order_left:
                switch (status) {
                    case 0:
                        break;
                    case 1:
                        MToast.makeTextLong(OrderDetailActivity.this, "取消订单").show();
                        handleOrderStatus.closeOrderApi(OrderDetailActivity.this, 7, orderDetailEntity.getId());
                        break;
                    case 2:
                        MToast.makeTextLong(OrderDetailActivity.this, "查看物流").show();
                        break;
                    case 3:
                        MToast.makeTextLong(OrderDetailActivity.this, "查看物流").show();
                        break;
                    case 4:
                        MToast.makeTextLong(OrderDetailActivity.this, "查看物流").show();
                        break;
                    case 5:
                        MToast.makeTextLong(OrderDetailActivity.this, "删除订单").show();
                        handleOrderStatus.closeOrderApi(OrderDetailActivity.this, 6, orderDetailEntity.getId());
                        break;
                    case 6:
                        break;
                    case 7:
                        break;

                }
                break;
            case R.id.tv_bottom_order_right:
                switch (status) {
                    case 0:
                        break;
                    case 1:
                        Intent ti = new Intent(OrderDetailActivity.this, PaidOrderActivity.class);
                        ti.putExtra("s_orderid", orderDetailEntity.getId());
                        ti.putExtra("allPrice", orderDetailEntity.getPrice() + "");
                        ti.putExtra("fromType", 1);
                        startActivity(ti);
//                        Intent intent = new Intent();
//                        intent.setAction(OrderActivity.orderAction);
//                        intent.putExtra("currentId", 2);
//                        sendBroadcast(intent);
                        break;
                    case 2:
                        break;
                    case 3:
                        handleOrderStatus.closeOrderApi(OrderDetailActivity.this, 5, orderDetailEntity.getId());
                        break;
                    case 4:
                        OrderListEntity orderListEntity = new OrderListEntity();
                        orderListEntity.setCount(orderDetailEntity.getCount());
                        orderListEntity.setExpressprice(orderDetailEntity.getExpressprice());
                        orderListEntity.setId(orderDetailEntity.getId());
                        orderListEntity.setOprice(orderDetailEntity.getOprice());
                        orderListEntity.setPrice(orderDetailEntity.getPrice());
                        orderListEntity.setProductid(orderDetailEntity.getProductid());
                        orderListEntity.setOrderid(orderDetailEntity.getOrderid());
                        orderListEntity.setProductimg(orderDetailEntity.getProductimg());
                        orderListEntity.setSpec(orderDetailEntity.getSpec());
                        orderListEntity.setState(orderDetailEntity.getState());

                        Intent ti2 = new Intent(OrderDetailActivity.this, CommentProductActivity.class);
                        ti2.putExtra("item", (Serializable) orderListEntity);
                        startActivityForResult(ti2,COMMENTSUCCESS);
                        break;
                    case 5:
                        Intent ti3 = new Intent(OrderDetailActivity.this, CommentProductDetailActivity.class);
                        ti3.putExtra("iid", orderDetailEntity.getCommentid());
                        startActivity(ti3);
                        break;
                    case 6:
                        break;
                    case 7:
                        break;

                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case COMMENTSUCCESS:
                    finish();
                    break;
            }
        }
    }
}
